package com.ky.home.controller;


import cn.hutool.core.convert.NumberWithFormat;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.home.entity.Words;
import com.ky.home.mapper.WordsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-21
 */
@RestController
@RequestMapping("/home")
public class WordsController {
    @Autowired
    WordsMapper wordsMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @RequestMapping("/words/getRandomWord")
    public R getRandomWord(HttpServletRequest request){
        //生成随机数
        // int i = RandomUtil.randomInt(1, 486);
        QueryWrapper<Words> wordsQueryWrapper = new QueryWrapper<>();
        String s = redisTemplate.opsForValue().get("wordBookId");
        //转化为int
        Integer.parseInt(s);
        wordsQueryWrapper.eq("book_id",s);
        List<Words> words = wordsMapper.selectList(wordsQueryWrapper);
        int size = words.size();
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        //redis存储的今日单词数自增
        redisTemplate.opsForValue().increment("todayWordNum"+id);
        int i = RandomUtil.randomInt(0, size);
        redisTemplate.opsForList().leftPush("wordList"+id,words.get(i).getWord() );
        //将出现过的所有单词存入redis的list类型中
        return R.ok(words.get(i));
    }
    @RequestMapping("/words/getTodayWordNum")
    public R getTodayWordNum(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        //获取redis中存储的今日单词数
        String s = redisTemplate.opsForValue().get("todayWordNum"+id);
        return R.ok(s);
    }
    //查找redis存储的以背的单词

    @RequestMapping("/words/recitation")
    public R recitation( HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        //获取redis中key为"wordList"+id的所有数据
        List<String> wordList = redisTemplate.opsForList().range("wordList"+id, 0, -1);
        List<Words> words1 = new ArrayList<>();
        for (String word : wordList) {
            System.out.println(word);
            words1.add(wordsMapper.selectOne(new QueryWrapper<Words>().eq("word", word)));
        }
        int i = RandomUtil.randomInt(0, words1.size());
        return R.ok(words1.get(i));
    }
    @RequestMapping("/words/recitationWordNum")
    public R recitationWordNum(HttpServletRequest request){
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        List<String> wordList = redisTemplate.opsForList().range("wordList"+id, 0, -1);
        return R.ok(wordList.size());
    }

}

